package com.example.accessingdatajpa.controller;

import com.example.accessingdatajpa.pojo.Picture;
import com.example.accessingdatajpa.service.IPictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/app")
public class PictureController {
    @Autowired
    final private IPictureService pictureService;

    public PictureController(IPictureService pictureService) {
        this.pictureService = pictureService;
    }

    //插入图片
    @PostMapping(path = "/uploadPic", produces = "image/png")
    @ResponseBody
    public void savePic(Picture picture, @RequestParam("file") MultipartFile file) throws IOException {
        pictureService.insertPic(picture,file);
    }

    //根据id查找图片
    @GetMapping(path = "/getPic/{pictureId}")
    @ResponseBody
    public void getPic(@PathVariable("pictureId") Long pictureId, HttpServletResponse response) throws IOException {
        pictureService.queryPicById(pictureId,response);
    }

    //根据id更换图片
    @PutMapping(path = "/updatePic/{pictureId}")
    @ResponseBody
    public void updatePic(@PathVariable("pictureId") Long pictureId,@RequestParam("file") MultipartFile file) throws IOException {
        pictureService.updatePicById(pictureId,file);
    }
}
